use crate::cloud_pkt::{CloudPacket, ProtoCloudHeader};
use crate::cloud_pkt::CPPayload::HeartbeatReq;
use crate::defs::CloudCmdType;

use crate::error::Error;

pub fn gen_heartbeat_req(vin: &str) -> Result<CloudPacket, Error> {

    let vin_num = <[u8; 17]>::try_from(vin.as_bytes());
    if vin_num.is_err() {
        return Err(Error::InvalidVIN(vin_num.unwrap_err().to_string()));
    }
    let mut pkt = gen_none_payload_pkt(CloudCmdType::VehicleHeartbeat.into(), vin_num.unwrap());
    pkt.set_payload(Some(HeartbeatReq));

    // let mut header = ProtoCloudHeader::new();
    // header.cmd_id = CloudCmdType::VehicleHeartbeat.into();
    // header.vin_id = vin_num.unwrap();
    //
    // let mut pkt = CloudPacket::new();
    // pkt.header = header;
    // pkt.set_payload(Some(HeartbeatReq));

    Ok(pkt)
}

pub fn gen_cal_time_req(vin: &str) -> Result<CloudPacket, Error> {
    let vin_num = <[u8; 17]>::try_from(vin.as_bytes());
    if vin_num.is_err() {
        return Err(Error::InvalidVIN(vin_num.unwrap_err().to_string()));
    }
    let mut pkt = gen_none_payload_pkt(CloudCmdType::VehicleHeartbeat.into(), vin_num.unwrap());
    pkt.set_payload(Some(HeartbeatReq));
    Ok(pkt)
}

pub fn gen_none_payload_pkt(cmd_id: u8, vin_num: [u8; 17]) -> CloudPacket {
    let mut header = ProtoCloudHeader::new();
    header.cmd_id = cmd_id;
    header.vin_id = vin_num;

    let mut pkt = CloudPacket::new();
    pkt.header = header;
    pkt.set_payload(None);
    pkt
}